<script setup lang="ts">
import {cFetch} from "~/composables/cFetch";

definePageMeta({
  //将登录页面独立开，不使用布局
  layout: false
})

// onMounted(() => {
//   const route = useRoute()
//   if (route.query.code) {
//     ElMessage.error(route.query.code + '' + route.query.message)
//   }
// })

import type {FormInstance, FormRules} from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const validateUsername = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the username'))
  }

    callback()
}

const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  username: '',
  password: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  username: [{validator: validateUsername, trigger: 'blur'}],
  password: [{validator: validatePassword, trigger: 'blur'}],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      cFetch<string>('/admin/login', {
        defaultOpts: {
          method: "POST",
          body: {
            username: ruleForm.username,
            password: ruleForm.password
          }
        }
      }).then(res => {
        if (res.code === 200) {
          userStore().setToken(res.data)
          ElMessage.success('登录成功')
          navigateTo('/')
        }
        // console.log(userStore().getToken())
      })
      // console.log('submit!')
    } else {
      ElMessage.warning('请根据提示完成登录信息的填写')
      console.log('error submit!')
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<template>
  <div class="content">
    <h2 class="title">顺风车后台系统</h2>

    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="rule-form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model.trim.lazy="ruleForm.username" autocomplete="off"/>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
            v-model.lazy="ruleForm.password"
            type="password"
            autocomplete="off"
        />
      </el-form-item>

      <div class="btn-area">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          登录
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">
          重置表单
        </el-button>
      </div>

    </el-form>
  </div>
</template>

<style scoped lang="scss">
.content {
  width: 98.4vw;
  height: 98vh;
  background-color: RGB(245, 247, 250);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.title {
  font-family: "Times New Roman", sans-serif;
  color: #18c9fd;
}

.btn-area {
  display: flex;
  justify-content: space-evenly;
}
</style>